package com.apobates.jforum.grief.aurl.convertor;

import com.apobates.jforum.grief.aurl.core.AUrlMeta;
import com.apobates.jforum.grief.aurl.core.AUrlMetaBuilder;
import com.apobates.jforum.grief.aurl.parser.PatternParser;
import com.apobates.jforum.grief.aurl.spring.CollectionUtils;
import com.apobates.jforum.grief.aurl.spring.StringUtils;

import java.util.Map;

/**
 * 继承BuilderConvertor的连接器. 服务于:AUrlMeta.join/link
 */
public class JoinConvertor extends BuilderConvertor{

    public AUrlMetaBuilder convert(AUrlMeta aUrlMeta, String other){
        if(!StringUtils.hasLength(other)){
            throw  new IllegalArgumentException("缺少必要的参数:1");
        }
        AUrlMeta base = new PatternParser().parse(other).build();
        final Map<String,String> baseMap = base.toMap();
        Map<String, String> differenceByKey = CollectionUtils.getDifferenceByKey(aUrlMeta.toMap(), baseMap);
        differenceByKey.putAll(baseMap);
        return this.createBuilder(differenceByKey);
    }

}
